<?php
namespace app\shouyin\admin;
use app\system\admin\Admin;
use app\system\model\systemUser as systemUserModel;
use app\user\model\User as UserModel;
use app\product\model\Product as ProductModel;
use app\shouyin\model\Deposit as DepositModel;
use think\Db;
class Deposit extends Admin
{
    protected $hisiModel = 'Deposit';//模型名称[通用添加、修改专用]
    protected $hisiTable = '';//表名称[通用添加、修改专用]
    protected $hisiAddScene = '';//添加数据验证场景名
    protected $hisiEditScene = '';//更新数据验证场景名
    protected function initialize()
    {
        parent::initialize();
        $admin = systemUserModel::get(ADMIN_ID);

        $this->assign('admin_type',$admin['type']);
        if ($this->request->action() != 'index' && !$this->request->isPost()) {
            $where = [];

            $where['shop_id'] = session('shop_id');

            // 会员
            $users = UserModel::getSelect(UserModel::all());
            $this->assign('users', $users);

            // 负责人
            $admins = systemUserModel::where($where)->all();
            $this->assign('admins', systemUserModel::getSelect($admins));

            // 产品
            $products = ProductModel::all();
            $this->assign('products', ProductModel::getSelect($products));

        }
    }
    public function index()
    {
        if ($this->request->isAjax()) {
            $where      = [];
            $page       = $this->request->param('page/d', 1);
            $limit      = $this->request->param('limit/d', 15);
            $data['data'] = DepositModel::where($where)
                            ->field('*,user_id as user_name, product_id as product_name, admin_id as admin_name')
                            ->page($page)
                            ->limit($limit)
                            ->order('id desc')
                            ->select();
            $data['count'] = DepositModel::where($where)->count('id');
            $data['code'] = 0;
            return json($data);
        }
        return $this->fetch();
    }

    public function take()
    {
        $id = $this->request->param('id');
        $formData = Db::name('deposit')->where('id', $id)->find();
        $formData['num_l'] = Db::name('deposit_log')->where('d_id',$formData['id'])->sum('product_num_l');
        $formData['num_s'] = $formData['num'] - $formData['num_l'];
        // hal
        if($this->request->isPost()){
            $postData        = $this->request->post();
            // $id         = $this->request->param('id');
            if (empty($postData['id'])) {
                return $this->axiosMsg('缺少id参数',false);
            }
            if($postData['product_num_l'] == 0){
                return $this->axiosMsg('领取数量不能为0',false);
            }
            if($postData['product_num_l'] > $formData['num_s']){
                return $this->axiosMsg('领取数量超过剩余数量',false);
            }
            if($formData['num_s'] - $postData['product_num_l'] == 0){
                $status = 2;
            }else{
                $status = 3;
            }
            $warehouse_stock = Db::name('warehouse')->where('shop_id',$formData['shop_id'])->column('type,id');
            Db::startTrans();
            try {
                $stockDuctArr = [];
                $stockDuctArr['sn'] = $formData['sn'];
                $stockDuctArr['product_id'] = $postData['product_id'];
                $stockDuctArr['product_num'] = $postData['product_num_l'];
                $stockDuctArr['product_name'] = $postData['product_name'];
                $stockDuctArr['shop_id'] = $formData['shop_id'];
                $stockDuctArr['type'] = '出库';
                $stockDuctArr['bak'] = '寄存单产品出库';
                $stockDuctArr['detailed_type'] = 2;
                $stockDuctArr['user_id'] = $formData['user_id'];
                $stockDuctArr['warehouse_id'] = $warehouse_stock[0];
                $stockDuctArr['createtime'] = time();
                $stockDuctArr['status'] = 1;
                $stockDuctArr['price'] = 0;
                Db::name('stock')->insert($stockDuctArr); 

                $postData['d_id']  = $postData['id'];
                unset($postData['id']);
                $postData['createtime'] =  date('Y-m-d H:i:s',time());
                Db::name('deposit_log')->insert($postData);   

                Db::name('deposit')->where('id',$id)->update(['status'=>$status]);
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                return $this->axiosMsg($e->getMessage(),false);
            }

            return $this->axiosMsg('领取成功！', true);
        }
        if(!$formData){
            return $this->error('订单不存在或权限不足');
        }
        $formData['user_name'] = Db::name('user')->where('id', $formData['user_id'])->value('nick');
        $formData['product_name'] = Db::name('product')->where('id', $formData['product_id'])->value('name');
        $this->assign('formData', $formData);
        

        return $this->fetch();
    }

    public function look()
    {
        $id = $this->request->param('id');
        $list = Db::name('deposit_log')->where('d_id',$id)->select();
        $this->assign('lists', $list);
        return $this->fetch();
    }
} 
